<template>
  <div class="r_block">
    <div
      v-for="(room, index) in rooms"
      :key="index"
      :span="6"
      class="r_block_card"
      :style="{
        backgroundColor: `${roomMap[room.room_type]['color']}`,
      }"
    >
      <div v-if="!room.isEditing" class="r_block_card_wh">
        <div class="r_block_card_wh_header">
          <a-icon
            class="r_block_card_wh_header_edit"
            type="edit"
            @click.stop="edit(index, room)"
          />
          <!-- <a-icon
            class="r_block_card_wh_header_delete"
            type="close"
            @click="deleteRoom(index, room)"
          /> -->
        </div>
        <div class="r_block_card_wh_body">
          <span class="r_block_card_wh_body_number">{{
            room.room_number
          }}</span>
          <span class="r_block_card_wh_body_name">{{ room.room_name }}</span>
        </div>
        <div class="r_block_card_wh_footer">
          <span class="r_block_card_wh_footer_tag">{{ room.room_type }}</span>
        </div>
        <div class="r_block_card_wh_icon">
          <a-icon :type="roomMap[room.room_type]['icon']" />
        </div>
      </div>
      <div v-else class="r_block_card_wh">
        <div class="r_block_card_wh_header">
          <a-icon 
            class="r_block_card_wh_header_edit"  
            type="check" 
            @click="endEdit(index, room)" 
          />
        </div>
        <div class="r_block_card_wh_body">
          <input
            type="text"
            placeholder="房号不能为空"
            v-model="room.room_number"
            class="r_block_card_wh_body_inputNum"
          />
          <input
            type="text"
            placeholder="房间名不能为空"
            v-model="room.room_name"
            class="r_block_card_wh_body_inputName"
          />
        </div>
        <div class="r_block_card_wh_footer">
          <select 
            class="r_block_card_wh_footer_select"
            v-model="room.room_type" 
            @change="changeType"
          >
            <option
              class="r_block_card_wh_footer_select_op"
              v-for="room_type in roomTypes"
              :key="room_type"
              :value="room_type"
            >
              {{ room_type }}
            </option>
          </select>
        </div>
        <div class="r_block_card_wh_icon">
          <a-icon :type="roomMap[room.room_type]['icon']" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axiosInstance from "@/utils/axios";

export default {
  props: ["roomsProp"],
  data() {
    return {
      roomMap: {
        教室: { name: "教室", color: "#96BFFF", icon: "home" },
        实验室: { name: "实验室", color: "#FFDB5C", icon: "experiment" },
        办公室: { name: "办公室", color: "#9FE6B8", icon: "bank" },
        其他: { name: "其他", color: "#ff9f7f", icon: "question" },
      },
      roomTypes: ["教室", "实验室", "办公室", "会议室", "报告厅", "其他"],
      checkedList: [],
      test: [],
    };
  },
  watch: {
    test: {
      immediate: true,
      handler(value) {
        this.test = value;
      },
    },
  },
  computed: {
    rooms() {
      return this.roomsProp.map((room) => {
        return {
          ...room,
          isEditing: false,
        };
      });
    },
  },
  methods: {
    onChange(e, value) {
      if (e.target.checked) {
        this.checkedList.push(value);
      } else {
        let index = this.checkedList.findIndex((item) => {
          return item === value;
        });
        this.checkedList.splice(index, 1);
      }
      console.log(this.checkedList);
    },
    edit(index, room) {
      this.rooms[index].isEditing = true;
      this.$forceUpdate();
    },
    async endEdit(index, room) {
      let url = `pc/v1/rooms/${room._id}`;
      try {
        await axiosInstance.patch(url, room);
        this.$message.info("编辑成功");
        this.rooms[index].isEditing = false;
        this.$forceUpdate();
      } catch (err) {
        console.log(err);
        this.$message.error("编辑失败");
      }
    },
    async deleteRoom(index, room) {
      let url = `pc/v1/rooms/${room._id}`;
      try {
        await axiosInstance.delete(url, room);
        this.$message.info("删除成功");
        this.rooms.splice(index, 1);
        this.$forceUpdate();
      } catch (err) {
        console.log(err);
        this.$message.error("删除失败");
      }
    },
    changeType() {
      this.$forceUpdate();
    },
  },
  mounted() {
    this.test = this.roomsProp;
  },
};
</script>

<style scoped>
.my-statistic-title {
  margin-bottom: 10px;
  color: #fff;
  font-size: 20px;
  letter-spacing: 3px;
}

.btn-area {
  padding: 20px 0 20px;
  display: flex;
  justify-content: space-between;
}

.card-container {
  padding-top: 20px;
}

.Card_Info {
  padding-top: 20px;
}

.editText {
  border: none;
  border-bottom: 1px solid #fff;
  outline: none;
  background-color: rgba(0, 0, 0, 0);
  width: 140px;
  margin: 0 0 14px 0;
  text-align: center;
}
</style>
